home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 726-750 / 729 / dsound / source / playstereo.c < prev   
C/C++ Source or Header  |  1995-03-18  |  5KB  |  195 lines

  1.  
  2. /*************************************************************************/
  3. /*                  PlayStereo.c                 */
  4. /*        Contains code used to play stereo samples         */
  5. /*************************************************************************/
  6.  
  7. #include <exec/types.h>
  8. #include <exec/exec.h>
  9. #include <devices/audio.h>
  10. #include <dos/dos.h>
  11. #include <intuition/intuition.h>
  12. #include <intuition/intuitionbase.h>
  13. #include <graphics/gfxbase.h>
  14. #include <stdlib.h>
  15.  
  16. #include "dsound.h"
  17.  
  18. #include <proto/intuition.h>
  19. #include <proto/exec.h>
  20. #include <proto/dos.h>
  21.  
  22. extern UBYTE rightAMap[];
  23. extern UBYTE leftAMap[];
  24. extern UBYTE eitherAMap[];
  25. extern UBYTE bothAMap[];
  26.  
  27. extern UBYTE volume;
  28. extern UWORD speed;
  29. extern ULONG bufSize;
  30.  
  31. extern BOOL readAll;
  32. extern BOOL loop;
  33. extern struct Window *window;
  34.  
  35. extern ULONG signalMask;
  36.  
  37. /*Play a stereo sample out of both speakers*/
  38. /*If the user specifies that just one of the two stereo channels will*/
  39. /*be played, DSound.c calls playMonoSample*/
  40.  
  41. void playStereoSample(BPTR leftFile,channel audioChannel,
  42.               struct Voice8Header *vhdr, ULONG length, char *filename)
  43. {
  44.    struct IOAudio *iob1_right,*iob2_right,*iob1_left,*iob2_left;
  45.    struct IOAudio *cur_right,*cur_left,*alt_right,*alt_left;
  46.    ULONG toRead;
  47.    ULONG sampleSize=length;
  48.    BOOL done=FALSE;
  49.    BPTR rightFile;
  50.  
  51.    /*Open the file again*/
  52.    rightFile=dupFileHandle(leftFile,filename);
  53.  
  54.    /*And position ourselves at the start of the right channel's data*/
  55.    Seek(rightFile,length,OFFSET_CURRENT);
  56.  
  57.    /*Read the entire sample into memory, if specified*/
  58.    if(readAll)
  59.    {
  60.       storeLeft(leftFile,length,bufSize);
  61.       storeRight(rightFile,length,bufSize);
  62.       Close(rightFile);
  63.       leftFile=0L;
  64.       rightFile=4L;
  65.    }
  66.  
  67.    /*Get the first audio channel*/
  68.    iob1_left=GetAudioChannel(bufSize,leftAMap);
  69.    if(iob1_left==NULL)
  70.    {
  71.       WriteMsg("Couldn't create the first stereo buffer\n");
  72.       cleanup(150);
  73.    }
  74.  
  75.    iob1_right=GetAudioChannel(bufSize,rightAMap);
  76.    if(iob1_right==NULL)
  77.    {
  78.       WriteMsg("Couldn't create the second stereo buffer\n");
  79.       cleanup(150);
  80.    }
  81.  
  82.    /* If the user didn't specify a volume, get it from the VHDR */
  83.    if(volume==0)
  84.       volume=(vhdr->volume>>10);
  85.  
  86.    /* If the VHDR gave a volume of zero, use maximum volume*/
  87.    if(volume==0)
  88.       volume=64;
  89.  
  90.    /* Get the samples/sec rate (either the rate given by the user, or the*/
  91.    /* rate found in the VHDR) */
  92.    if(speed==0)
  93.       speed=1000000000/(vhdr->samplesPerSec*279);
  94.    else
  95.       speed=1000000000/(speed*279);
  96.  
  97.    InitAudioChannel(iob1_left,volume,speed);
  98.    InitAudioChannel(iob1_right,volume,speed);
  99.  
  100.    /*Get the 2nd audio channel*/
  101.    iob2_left=DuplicateAudioChannel(iob1_left);
  102.  
  103.    if(iob2_left==NULL)
  104.    {
  105.       FreeAudioChannel(iob1_left);
  106.       FreeAudioChannel(iob1_right);
  107.       WriteMsg("Couldn't create the second buffer");
  108.       cleanup(175);
  109.    }
  110.  
  111.    iob2_right=DuplicateAudioChannel(iob1_right);
  112.    if(iob2_right==NULL)
  113.    {
  114.       FreeAudioChannel(iob1_left);
  115.       DeleteDuplication(iob2_left);
  116.       FreeAudioChannel(iob1_right);
  117.       WriteMsg("Couldn't create the second buffer");
  118.       cleanup(175);
  119.    }
  120.  
  121.    /* Load the first buffer*/
  122.    toRead=MIN(length,bufSize);
  123.    LoadAudioBuffer(leftFile,iob1_left,toRead);
  124.    LoadAudioBuffer(rightFile,iob1_right,toRead);
  125.    iob1_left->ioa_Length=iob1_right->ioa_Length=toRead;
  126.  
  127.    length-=toRead;
  128.    if(length==0 && loop)
  129.    {
  130.       length=sampleSize;
  131.       Seek(leftFile,-sampleSize,OFFSET_CURRENT);
  132.       Seek(rightFile,-sampleSize,OFFSET_CURRENT);
  133.    }
  134.  
  135.    /*And queue up the play requests*/
  136.    BeginIO((struct IORequest *)iob1_left);
  137.    BeginIO((struct IORequest *)iob1_right);
  138.  
  139.    cur_right=iob2_right;
  140.    cur_left=iob2_left;
  141.    alt_right=iob1_right;
  142.    alt_left=iob1_left;
  143.  
  144.    /*Loop while there's stuff to read*/
  145.    while(!done)
  146.    {
  147.       toRead=MIN(length,bufSize);
  148.  
  149.       if(toRead!=0)
  150.       {
  151.      LoadAudioBuffer(leftFile,cur_left,toRead);
  152.      LoadAudioBuffer(rightFile,cur_right,toRead);
  153.      cur_left->ioa_Length=cur_right->ioa_Length=toRead;
  154.      BeginIO((struct IORequest *)cur_left);
  155.      BeginIO((struct IORequest *)cur_right);
  156.      length-=toRead;
  157.  
  158.      if(length==0 && loop)
  159.      {
  160.         length=sampleSize;
  161.         Seek(leftFile,-sampleSize,OFFSET_CURRENT);
  162.         Seek(rightFile,-sampleSize,OFFSET_CURRENT);
  163.      }
  164.      done=FALSE;
  165.       }
  166.       else
  167.      done=TRUE;
  168.  
  169.       /*Wait for the buffer to finish*/
  170.       if((Wait(1<<cur_right->ioa_Request.io_Message.mn_ReplyPort->mp_SigBit |
  171.        signalMask) & SIGBREAKF_CTRL_C) == SIGBREAKF_CTRL_C)
  172.      done=TRUE;
  173.  
  174.       swapPointers(&cur_left,&alt_left);
  175.       swapPointers(&cur_right,&alt_right);
  176.    }
  177.  
  178.    /*Restore the buffer lengths, so that FreeAudio() channel, etc., knows*/
  179.    /*how much memory to free*/
  180.    iob1_left->ioa_Length=iob2_left->ioa_Length=bufSize;
  181.    iob1_right->ioa_Length=iob2_right->ioa_Length=bufSize;
  182.  
  183.    FreeAudioChannel(iob1_left);
  184.    DeleteDuplication(iob2_left);
  185.    FreeAudioChannel(iob1_right);
  186.    DeleteDuplication(iob2_right);
  187.  
  188.    if(rightFile != 4L)
  189.       Close(rightFile);
  190.  
  191.    return;
  192. }
  193.  
  194. /*End of PlayStereo.c*/
  195.